package com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.DoctorService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.DoctorDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.DoctorListDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

@Component("doctorService")
public class DoctorServiceImpl extends BaseService implements DoctorService {
	@Autowired
	private CommonDAO commonDAO;

	public void setCommonDAO(CommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}

	@Override
	public Map<String, Object> addDoctor(JSONObject transferJson)
			throws BusinessServiceException {
		DoctorDTO doctorDTO = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			doctorDTO = JSONObject.toJavaObject(transferJson, DoctorDTO.class);
			String id = commonDAO.getUUID();
			doctorDTO.setId(id);
			commonDAO.insert("healthcare.addDoctor", doctorDTO);

			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.INSERT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateDoctor(JSONObject transferJson)
			throws BusinessServiceException {
		DoctorDTO doctorDTO = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			doctorDTO = JSONObject.toJavaObject(transferJson, DoctorDTO.class);
			commonDAO.update("healthcare.updateDoctor", doctorDTO);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.UPDATE_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteDoctor(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, String> parms = new HashMap<String, String>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String ids = transferJson.getString("id");
			StringBuffer sb = new StringBuffer();
			String[] id = ids.split(",");
			for (int i = 0; i < id.length; i++) {
				sb.append("'" + id[i] + "',");
			}
			ids = sb.toString().substring(0, sb.toString().lastIndexOf(','));
			parms.put("ids", ids);
			parms.put("updateby", transferJson.getString("updateby"));
			commonDAO.update("healthcare.deleteDoctor", parms);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.DELETE_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> queryDoctorList(JSONObject transferJson)
			throws BusinessServiceException {
		DoctorListDTO doctorListDTO = new DoctorListDTO();
		Map<String, Object> result = null;
		try {
			doctorListDTO.setQueryParams(JSONObject.toJavaObject(transferJson,
					Map.class));
			doctorListDTO.setPage(Integer.parseInt((String) transferJson
					.get("page")));
			doctorListDTO.setPageSize(Integer.parseInt((String) transferJson
					.get("pageSize")));
			doctorListDTO.setDoCount(true);
			doctorListDTO.setQueryAll(false);

			result = (Map<String, Object>) commonDAO.queryPage(
					"healthcare.queryDoctorList", doctorListDTO);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> queryDoctorDetail(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			DoctorDTO doctorDTO = null;
			doctorDTO = JSONObject.toJavaObject(transferJson, DoctorDTO.class);
			result = (Map<String, Object>) commonDAO.get(
					"healthcare.queryDoctorDetail", doctorDTO);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
			resultMap.put(Constants.SUCCESS_RESULT_KEY, result);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

}
